# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        if not head:
           return head
        while head.val == val:
            if not head.next:
                return
            head = head.next
        a = head
        b = head.next
        while b:
            if b.val == val:
                a.next = b.next
                b = b.next
            else:
                a = b
                b = b.next
        return head